package top.igotcha.chapter.chapter7.ForkAndJoin;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

/**
 * @author Gotcha
 * @date 2021/3/15
 * @describe
 */
@Slf4j(topic = "ForkAndJoinTest01")
public class ForkAndJoinTest01  {
    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool(4);
        System.out.println(pool.invoke(new MyTask(5)));
    }

    static class MyTask extends RecursiveTask<Integer>{
        private int n;

        public MyTask(int n){
            this.n = n;
        }

        @Override
        public String toString() {
            return "{" + n + '}';
        }
        @Override
        protected Integer compute() {
            // 如果 n 已经为 1，可以求得结果了
            if (n == 1) {
                log.debug("join() {}", n);
                return n;
            }
            // 将任务进行拆分(fork)
            MyTask t1 = new MyTask(n - 1);
            t1.fork();
            log.debug("fork() {} + {}", n, t1);
            // 合并(join)结果
            int result = n + t1.join();
            log.debug("join() {} + {} = {}", n, t1, result);
            return result;
        }
    }
}
